<?php
namespace app\admin\controller;
use think\Config;
use think\Controller;
use think\Db;
//管理员(站长)模块
class Admin extends Allow{
	//管理员列表
	public function getIndex(){
		$request=request();
		$k = $request->param("k");//获取搜索值 k
		
		$data = Db::table("admin_user")->where("loginname","like","%".$k."%")->paginate(2);
		return $this->fetch("Admin/index",["data"=>$data,"k"=>$k,"data2"=>$request->param()]);//引入列表模版
	}

	//引入添加页面
	public function getAdd(){
		return $this->fetch("Admin/add");
	}

	//执行添加
	public function postAdddo(){
		$request = request();
		$res = $this->validate($request->param(),"Admin");//引入验证器
		if($res!==true){
			$this->error($res,"/adminadmin/add");
		}
		$data = $request->only(['loginname','password','adminname','rid']);
		$res = Db::table('admin_user')->insert($data);
		if($res){
				$this->success("ok","/adminadmin/index");
			}else{
				$this->error("on","/adminadmin/add");
			}
		}
	

	//执行删除
	public function getDelete(){
		$request = request();
		$id = $request->param("id");

		if($id==1){
			$this->error("无法删除超级管理员","/adminadmin/index");
		}else{
			$res = Db::table("admin_user")->delete($id);
				if($res){
						$this->success("ok","/adminadmin/index");
					}else{
						$this->error("on","/adminadmin/index");
					}
				}
		}
	//执行修改



	//激活或禁用
	public function getStatus(){
		$request=request();
		$status = Db::table("admin_user")->where("id",$request->param("id"))->value("status");

		if($request->param("id")==1){
			$this->error("无法禁用超级管理员","/adminadmin/index");
		}

		if($status =='1'){
			$res = Db::table("admin_user")->where('id',$request->param("id"))->update(['status'=>'0']);
			if($res){
				$this->success("ok","/adminadmin/index");
			}else{
				$this->error("on","/adminadmin/index");
			}
		}else{
			$res = Db::table("admin_user")->where('id',$request->param("id"))->update(['status'=>'1']);
			if($res){
				$this->success("ok","/adminadmin/index");
			}else{
				$this->error("on","/adminadmin/index");
			}
		}
	}
}
